package blancosMikel.model.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import blancosMikel.controller.beans.detalleProductosBeans;

public class detalleProductosDAO extends SigaabmDAO{
	StringBuilder query = new StringBuilder();

	
	public ArrayList<detalleProductosBeans> consultaTodoDetallesProductoPorProducto(int id_productos) throws SQLException{
		ArrayList<detalleProductosBeans> lista = new ArrayList<detalleProductosBeans>();
		detalleProductosBeans bean;
		try {
			conn = this.getConnectionMYSQL();
			query.append("SELECT * FROM ").append("detalleproducto").append(" WHERE id_productos = "+id_productos +" ;");
			conn.prepareStatement(query.toString());
			rs = ps.executeQuery();
			while (rs.next()){
				bean = new detalleProductosBeans();
				bean.setId_detalleProducto(rs.getInt("id_detalleProducto"));
				bean.setId_productos(rs.getInt("id_productos"));
				bean.setFolios(rs.getString("folios"));
				bean.setCosto_compra(rs.getDouble("costo_compra"));
				bean.setCosto_venta(rs.getDouble("costo_venta"));
				bean.setStock(rs.getInt("stock"));
				bean.setSize(rs.getString("size"));
				lista.add(bean);
			}
			conn.close();	
		} catch (SQLException sql) {
		} finally{
			rs.close();
			ps.close();
			conn.close();
		}
		return lista;	
	}
	
	public detalleProductosBeans consultaDetalle(int id) throws SQLException{
		detalleProductosBeans bean = null;
		try {
			conn = this.getConnectionMYSQL();
			query.append("SELECT * FROM ").append("detalleproducto")
			.append(" WHERE id_detalleproducto =").append(id).append(";");
			conn.prepareStatement(query.toString());
			rs = ps.executeQuery();
			if(rs.next()){
				bean = new detalleProductosBeans();
				bean.setId_detalleProducto(rs.getInt("id_detalleProducto"));
				bean.setId_productos(rs.getInt("id_productos"));
				bean.setFolios(rs.getString("folios"));
				bean.setCosto_compra(rs.getDouble("costo_compra"));
				bean.setCosto_venta(rs.getDouble("costo_venta"));
				bean.setStock(rs.getInt("stock"));
				bean.setSize(rs.getString("size"));
			}
		} catch (SQLException sql) {
		}finally{
			rs.close();
			ps.close();
			conn.close();
		}
		return bean;
	}
	
	public void eliminarDetalleProducto(int id) throws SQLException{
		try {
			conn = this.getConnectionMYSQL();
			query.append("DELETE FROM ").append("detalleproducto")
			.append(" WHERE id_detalleproducto =").append(id).append(";");
			conn.prepareStatement(query.toString());
			ps.executeUpdate();
	    }catch (SQLException sql) {	    	
	    }finally{
			rs.close();
			ps.close();
			conn.close();
		}
	}
	
	public void insertaDetalleProducto(detalleProductosBeans bean) throws SQLException{
		try {
			conn = this.getConnectionMYSQL();
			query.append("INSERT INTO ").append("detalleProducto")
			.append(" VALUES(").append(bean.getId_detalleProducto())
			.append(",").append(bean.getId_productos())
			.append(",'").append(bean.getFolios()).append("'")
			.append(",").append(bean.getCosto_compra())
			.append(",").append(bean.getCosto_venta())
			.append(",").append(bean.getStock())
			.append(",'").append(bean.getSize()).append("'")
			.append(")").append(";");
			conn.prepareStatement(query.toString());
			ps.executeUpdate();
	    }catch (SQLException sql) {	    	
	    }finally{
			rs.close();
			ps.close();
			conn.close();
		}
	}
}
